/*
 * @Description: olcs 3D 地图入口文件
 * @Date: 2022-01-05 10:13:02
 * @Author: yeming
 */

/* eslint-disable space-before-function-paren */
import OLCesium from 'olcs/OLCesium.js' // 中间过程会加载cesium，需要配置 cesium
import PerformanceDisplay from 'cesium/Scene/PerformanceDisplay.js'
require('olcs/olcs.css')

class Ol3d {
  constructor(ol2d, options = {}) {
    this.map3d = new OLCesium({ map: ol2d.map2d, target: options.target3d })
    this.map3d.setEnabled(options.enabled) // true 开放3D false 隐藏3D
    this.camera = this.map3d.getCamera()
    // 渲染前DataSourceDisplay就拿到所有的Visualizer（可视化生产器  理解成模子）
    this.entities = this.map3d.getDataSourceDisplay().defaultDataSource.entities
    this.scene = this.map3d.getCesiumScene()
    // 深度检测（如果不开启，内容物会飘，如果开启，传入相对高度，内容物会被埋入地下）
    this.scene.globe.depthTestAgainstTerrain = false // 深度检测（如果不开启，内容物会飘）
    this.scene.globe.enableLighting = true
    this.scene.globe.tileCacheSize = 800
    this.ellipsoid = this.scene.globe.ellipsoid
    // 设置分辨率缩放
    var vtxfDpr = window.devicePixelRatio
    if (vtxfDpr != null) {
      while (vtxfDpr >= 2.0) { vtxfDpr /= 2.0 }
      this.map3d.setResolutionScale(vtxfDpr)
    }

    // 3D地图加载显示帧率
    const fpsMain = document.getElementById('fpsMain')
    this.scene.groundPrimitives.add(new PerformanceDisplay({ container: fpsMain }))

    // 3D 瓦片高程数据
    // this.scene.terrainProvider = window.Cesium.createWorldTerrain() //  官方服务器
    // this.scene.terrainProvider = new Cesium.CesiumTerrainProvider({
    //   url: 'http://192.168.18.235:9099/tms/terrain/wgs84/WEB_D6y3CD4L14Vqv-6641-b72d5b255915839c67a9c6f11ad9ec28'
    // })

    // 3d 屏幕事件
    this.eventHandler3d = new Cesium.ScreenSpaceEventHandler(this.scene.canvas)
  }

  /**
   * @desc: 设置地图模式 0-只读 1-开放 2-绘图
   * @desc: enableRotate-旋转相机 enableTranslate-平移地图 enableZoom-放大和缩小 enableTilt-倾斜相机
   * @param {Number} type
   * @return {*}
   * @use: 使用示例
   */
  setMapSceneScreenSpaceCameraController (type) {
    if (type <= 0) {
      this.scene.screenSpaceCameraController.enableRotate = false;
      this.scene.screenSpaceCameraController.enableTranslate = false;
      this.scene.screenSpaceCameraController.enableZoom = false;
      this.scene.screenSpaceCameraController.enableTilt = false;
    } else {
      this.scene.screenSpaceCameraController.enableRotate = true;
      this.scene.screenSpaceCameraController.enableTranslate = true;
      this.scene.screenSpaceCameraController.enableZoom = true;
      this.scene.screenSpaceCameraController.enableTilt = true;
    }
  }

  // 设置是否显示3d
  setMapType3dEnabled = function (enabled) {
    this.map3d.setEnabled(enabled)
  }
  // 获取当前是否显示3d
  get3dMapEnabled = function () {
    return this.map3d.getEnabled()
  }
}

export default Ol3d

